﻿using System;
using System.Collections.Generic;
using System.Text;
using AppContext = T.ServiceComponent.Session.Service.AppContext;

namespace T.FAS.Runtime.Base.Common
{
    public static class UserUtil
    {
        /// <summary>
        /// 获取当前登录的用户ID
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserID()
        {
            string userID;
            try
            {
                if (LiveDebug.IsDebug)
                    return LiveDebug.MockUserId;
                userID = Convert.ToString(AppContext.Current.Session?["UserId"]);
            }
            catch
            {
                userID = "T";
            }
            return userID;
        }

        /// <summary>
        /// 获取当前登录用户的名称
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserName(string defaultUserName = "")
        {
            string userName;
            try
            {
                userName = Convert.ToString(AppContext.Current.Session?["UserName"]);
                userName = !String.IsNullOrEmpty(userName) ? userName : Convert.ToString(AppContext.Current.Session?["Account"]);
                //如果用户名为空  且 默认用户名不为空 那么采用默认用户名
                if (string.IsNullOrEmpty(userName))
                {
                    if (!string.IsNullOrEmpty(defaultUserName))
                    {
                        userName = defaultUserName;
                    }
                    else
                    {
                        userName = "T";
                    }
                }
            }
            catch
            {
                if (string.IsNullOrEmpty(defaultUserName))
                {
                    userName = "T";
                }
                else
                {
                    userName = defaultUserName;
                }
            }
            return userName;
        }
        /// <summary>
        /// 获取当前登录用户的租户ID
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserTenantId()
        {
            string userID;
            try
            {
                userID = Convert.ToString(AppContext.Current.Session["CompanyId"]);
            }
            catch
            {
                userID = "T";
            }
            return userID;
        }
        /// <summary>
        /// 获取当前登录用户的 部门
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentUserDepartmentID()
        {
            string dept;
            try
            {
                dept = Convert.ToString(AppContext.Current.Session["DepartmentId"]);
            }
            catch
            {
                dept = "T";
            }

            if (string.IsNullOrEmpty(dept))
            {
                dept = "T";
            }
            return dept;
        }
    }
}
